package com.xiaozhi.libraryfunction.controller;

import com.xiaozhi.common.annotation.Log;
import com.xiaozhi.common.constant.UserConstants;
import com.xiaozhi.common.core.controller.BaseController;
import com.xiaozhi.common.core.domain.AjaxResult;
import com.xiaozhi.common.core.page.TableDataInfo;
import com.xiaozhi.common.enums.BusinessType;
import com.xiaozhi.common.utils.StringUtils;
import com.xiaozhi.common.utils.poi.ExcelUtil;
import com.xiaozhi.libraryfunction.domain.LibraryBook;
import com.xiaozhi.libraryfunction.domain.LibraryBorrow;
import com.xiaozhi.libraryfunction.domain.LibraryType;
import com.xiaozhi.libraryfunction.service.LibraryBookService;
import com.xiaozhi.libraryfunction.service.LibraryBorrowService;
import com.xiaozhi.libraryfunction.service.LibraryTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 借阅信息操作处理
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/admin/library/borrow")
public class LibraryBorrowController extends BaseController
{
    private String prefix = "library/borrow";

    @Autowired
    private LibraryBorrowService libraryBorrowService;
    @Autowired
    private LibraryTypeService libraryTypeService;
    @Autowired
    private LibraryBookService libraryBookService;

    @RequiresPermissions("library:borrow:view")
    @GetMapping()
    public String LibraryBorrow(ModelMap mmap)
    {
        mmap.put("libraryTypes",libraryTypeService.selectLibraryTypeList(new LibraryType()));
        return prefix + "/borrow";
    }


    @RequiresPermissions("library:borrow:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LibraryBorrow borrow,String user_name,String name,Integer typeId)
    {
        startPage();
        List<LibraryBorrow> list = libraryBorrowService.selectLibraryBorrowList(borrow,user_name,name,typeId);
        return getDataTable(list);
    }
    @Log(title = "借阅管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("library:borrow:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LibraryBorrow borrow,String user_name,String name,Integer typeId)
    {
        List<LibraryBorrow> list = libraryBorrowService.selectLibraryBorrowList(borrow,user_name,name,typeId);
        ExcelUtil<LibraryBorrow> util = new ExcelUtil<LibraryBorrow>(LibraryBorrow.class);
        return util.exportExcel(list, "图书借阅");
    }

    /**
     * 新增借阅
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("libraryTypes",libraryTypeService.selectLibraryTypeList(new LibraryType()));
        return prefix + "/add";
    }

    /**
     * 新增保存借阅
     */
    @RequiresPermissions("library:borrow:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addBorrowSave(@RequestParam("bookId")List<Long> bookId,@RequestParam("userId")Long userId)
    {
        String msg = "借阅";
        for (Long bookid : bookId) {
            LibraryBorrow info = libraryBorrowService.checkLibraryBorrowBookidTouserIdUnique(bookid, userId);
            if (!StringUtils.isNotNull(info) || info.getBackType() !=4 ) {
                //判断库存是否还有
                LibraryBook libraryBook = libraryBookService.selectLibraryBookById(bookid);
                if (libraryBook.getCount()>0){
                    LibraryBorrow libraryBorrow = new LibraryBorrow();
                    libraryBorrow.setBookId(bookid);
                    libraryBorrow.setUserId(userId);
                    libraryBorrowService.insertLibraryBorrow(libraryBorrow);
                    //图书数量-1
                    libraryBookService.updateLibraryBookCut(bookid);
                }

            }else {
                msg = msg + "<a href='javascript:void(0)' style='color:#ed5565'>"+info.getLibraryBook().getName()+"</a><br>";
            }
        }
        if ("借阅".equals(msg)){
            return success("<a href='javascript:void(0)' style='color:#18a689'>借阅成功</a>");
        }
        return error(msg+"失败，此书已借阅");
    }

    /**
     * 修改借阅
     */
    @RequiresPermissions("library:borrow:edit")
    @GetMapping("/edit/{borrowId}")
    public String typeedit(@PathVariable("borrowId") Long borrowId, ModelMap mmap)
    {
        mmap.put("libraryborrow", libraryBorrowService.selectLibraryBorrowById(borrowId));
        return prefix + "/edit";
    }

    /**
     * 修改保存借阅还书
     */
    @RequiresPermissions("library:borrow:edit")
    @Log(title = "借阅管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated LibraryBorrow borrow)
    {
        int i = libraryBorrowService.updateLibrarybackBorrow(borrow);
        if (i>0){
            libraryBookService.updateLibraryBookAdd(borrow.getBookId());
        }
        return toAjax(i);
    }

    @RequiresPermissions("library:borrow:remove")
    @Log(title = "借阅管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(libraryBorrowService.deleteLibraryBorrowByIds(ids));
    }

    /**
     * 个人借阅页面
     */
    @RequiresPermissions("library:borrow:person:view")
    @GetMapping("/person")
    public String personLibraryBorrow(ModelMap mmap)
    {
        mmap.put("libraryTypes",libraryTypeService.selectLibraryTypeList(new LibraryType()));
        return prefix + "/borrowperson";
    }

    /**
     * 个人借阅页面查询
     */
    @RequiresPermissions("library:borrow:person:list")
    @PostMapping("/person/list")
    @ResponseBody
    public TableDataInfo personlist(LibraryBorrow borrow,String user_name,String name,Integer typeId)
    {
        startPage();
        List<LibraryBorrow> list = libraryBorrowService.selectLibraryBorrowListPerson(borrow,user_name,name,typeId, Math.toIntExact(getUserId()));
        return getDataTable(list);
    }
}
